﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Orid.Management.Statistics.Tools.Decomposition.Logic
{
    public enum WorkMode
    {
        Offline, Online
    }

    public enum Category
    {        
        FrameObject =10,
        BlockObject=11,
        JudgeObject=12,
        InputObject=13,
        非文字 = 0,
	    手書=2,
        FrameResult =14,
        ParentObject =15, //2011-3-19 add
    }
    public enum JobClass
    {
        
        判定ジョブ	= 1,
        ABジョブ	=2,
        CDジョブ	=3,
        Eジョブ	=4,
        MTジョブ	=6,
        Trimerジョブ	=7,
        OCRジョブ	=8,
        候補結果判定ジョブ	=9,
        新ABジョブ	=10,
        新Dジョブ	=11,
        新Eジョブ	=12,
        EntryFinalジョブ	=14,
        機械仕分ジョブ = 15,
        手動仕分ジョブ = 13,
        Fジョブ = 16,//名刺结果E抽取
        Gジョブ = 17,//名刺结果F抽取

    }

    public enum JobClassAll
    {
        unKnow, Frame, Block, MT, Trimer, OCR, α, β, A, B, C, D, E, Final, 機械仕分, 手動仕分, F, G, 判定A, 判定B
    }
 
    public enum WordProperty
    {        
        非文字=0,	手書=2
    }

    public enum OrderState
    {
        Finished,UnFinished
    }

    public class Log
    {
       
        // public static log4net.ILog log ;
         private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); 

        ///// 
        ///// 动态生成Log日志的名称
        ///// 
        /////ilog对象
        ///// 日志的前缀
        //public static void ChangeLogFileName(string fileName)
        //{
        //    log4net.Core.LogImpl logImpl = (log4net.Core.LogImpl)log;
        //    if (logImpl != null)
        //    {
        //        log4net.Appender.AppenderCollection ac = ((log4net.Repository.Hierarchy.Hierarchy)(((log4net.Core.LoggerWrapperImpl)(log)).Logger.Repository)).Root.Appenders;
        //        for (int i = 0; i < ac.Count; i++)
        //        {    //这里我只对RollingFileAppender类型做修改
        //            log4net.Appender.RollingFileAppender rfa = ac[i] as log4net.Appender.RollingFileAppender;

        //            if (rfa != null)
        //            {
        //                fileName = AppDomain.CurrentDomain.BaseDirectory + @"\log\" + fileName + DateTime.Now.ToString("yyyyMMdd") + ".log";
        //                rfa.File = fileName;
        //                if (!System.IO.File.Exists(fileName))
        //                {
        //                    System.IO.File.AppendAllText(fileName, "");
        //                }

        //                //更新Writer属性
        //                rfa.Writer = new System.IO.StreamWriter(rfa.File, rfa.AppendToFile, rfa.Encoding);
        //            }
        //        }
        //    }
        //}

         public static void Error(string Message, Exception ex)
         {
             log.Error(Message, ex);
         }

         public static void Error(string Message)
         {
             log.Error(Message);
         }

        public static void Info(string Message, Exception ex)
        {
            log.Info(Message, ex);
        }
        public static void Info(string Message)
        {
            log.Info(Message);
        }

        public static void Warn(string Message, Exception ex)
        {
            log.Warn(Message, ex);
        }

        public static void Warn(string Message)
        {
            log.Warn(Message);
        }

        public   static void initLog4Net()
        {
            //注意这里不写全路径可能导致无法找到配置文件
            FileInfo file = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config");
            if (File.Exists(file.ToString()))
            {
                log4net.Config.XmlConfigurator.Configure(file);
            }
            else
            {
                throw new Exception("log4net日志配置文件未找到");
            }
        }

       
    }

    [Obsolete("不推荐在使用")]
    public class CustomerConvert
    {
        public static string _Convert(Guid value)
        {
            if (value == null)
            {
                return "null";
            }
            else
            {
                return "'" + value + "'";
            }
        }
        public static string _Convert(string value)
        {
            if ((value == null) || (value.Equals(string.Empty)))
            {
                return "''";
            }
            else
            {
                return "'" + value + "'";
            }
        }
        public static string _Convert(DateTime? value)
        {
            if ((value == null))
            {
                return "null";
            }
            else
            {
                return "'" + value + "'";
            }
        }

        public static string _ChangeChat(string value)
        {
            //换行符 替换成空格
            return value.Replace("\n", " ");
        }
    }

    public class SimpleSerializer
    {

        /// <summary>
        /// 序列化对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="t">对象</param>
        /// <returns></returns>
        public static string Serialize<T>(T t)
        {

            using (StringWriter sw = new StringWriter())
            {

                XmlSerializer xz = new XmlSerializer(t.GetType());

                xz.Serialize(sw, t);

                return sw.ToString();

            }

        }



        /// <summary>
        /// 反序列化为对象
        /// </summary>
        /// <param name="type">对象类型</param>
        /// <param name="s">对象序列化后的Xml字符串</param>
        /// <returns></returns>
        public static object Deserialize(Type type, string s)
        {

            using (StringReader sr = new StringReader(s))
            {

                XmlSerializer xz = new XmlSerializer(type);

                return xz.Deserialize(sr);

            }

        }


        

    }


    public class SplitList
    {
        /// <summary>
        /// 将一个List分割成指定段数
        /// </summary>
        /// <typeparam name="T">List中储存的实例类型</typeparam>
        /// <param name="sourceList">要进行分段的List</param>
        /// <param name="splitSize">要分割成的段数</param>
        /// <returns>分割结果集</returns>
        public static  List<List<T>> Split<T>(List<T> sourceList, int splitCount)
        {
            List<List<T>> result = new List<List<T>>();
            int splitSize = sourceList.Count / splitCount; //长度
            int remainder = splitSize > 0 ? sourceList.Count % splitCount :
sourceList.Count; //余数
            for (int i = 0; i < splitCount; i++)
            {
                result.Add(sourceList.GetRange(i * splitSize, splitSize));
            }
            for (int i = 0; i < remainder; i++)
            {
                result[i].Add(sourceList[splitSize * splitCount + i]);
            }
            return result;
        }
    }
     
     public class Util
     {

         #region 获得数据库访问地址

         /// <summary>
         /// Gets the server address.
         /// </summary>
         /// <value>The server address.</value>
         public static string ServerAddress
         {
           

             get
             {
                 WorkMode workMode = WorkMode.Offline;

                if (!Convert.ToBoolean(Config.Setting.SettingInstance.Offline))
                {
                    workMode = WorkMode.Online;
                }

                 string memo = string.Empty;
                 if (workMode == WorkMode.Offline)
                 {
                     memo = Config.Setting.SettingInstance.DBAddress_Offline;
                 }
                 else
                 { memo = Config.Setting.SettingInstance.DBAddress_Online; }

                 memo = memo.Substring(0, memo.IndexOf(";"));

                 return memo;
             }
         }

         #endregion
     }
}
